package com.ttc.group2.videochat.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.quickblox.users.model.QBUser;
import com.ttc.group2.videochat.R;
import com.ttc.group2.videochat.controller.OverflowPeopleSelectedListener;
import com.ttc.group2.videochat.model.Section;

public class PeopleListAdapter extends BaseAdapter {

	private static final String TAG = "PeopleListAdapter";
	private List<QBUser> rows;

	// set data
	public void setRows(List<QBUser> rows) {
		this.rows = rows;
	}

	// set data filter by friends list
	public void filterByFriends(List<QBUser> friends) {
		this.rows = friends;
		notifyDataSetChanged();
	}

	// set data filter by available online
	public void filterByOnline(List<QBUser> usersOnline) {
		this.rows = usersOnline;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (rows != null) {
			return rows.size();
		}
		return 0;
	}

	@Override
	public QBUser getItem(int position) {
		if (rows != null) {
			return rows.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		if (getItem(position) instanceof Section) {
			return 1;
		} else {
			return 0;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (getItemViewType(position) == 0) { // Item people
			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) parent.getContext()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.people_item, parent,
						false);
				holder = new ViewHolder(convertView);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.bind(getItem(position));

		} else { // Section
			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) parent.getContext()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(
						R.layout.people_letter_list_item, parent, false);
			}

			Section section = (Section) getItem(position);
			TextView textView = (TextView) convertView
					.findViewById(R.id.letter_title);
			textView.setText(section.text);
		}

		return convertView;
	}

	private static boolean isAvailable(QBUser user) {
		long currentTime = System.currentTimeMillis();
		long userLastRequestAtTime = user.getLastRequestAt().getTime();

		// if user don't do anything last 5 minutes
		if ((currentTime - userLastRequestAtTime) > 5 * 60 * 1000) {
			return false;
		}
		return true;
	}

	static class ViewHolder {
		ImageView avatar;
		ImageView icon_avaiable;
		ImageView overflow_people;
		TextView people_name;
		TextView message;
		View base;

		public ViewHolder(View view) {
			this.base = view;
			avatar = (ImageView) base.findViewById(R.id.people_avatar);
			icon_avaiable = (ImageView) base.findViewById(R.id.icon_online);
			overflow_people = (ImageView) base
					.findViewById(R.id.overflow_people_selected);
			overflow_people.setColorFilter(R.color.videochat_black);
			people_name = (TextView) base.findViewById(R.id.people_item_title);
			message = (TextView) base.findViewById(R.id.people_item_status);
			base.setTag(this);
		}

		public void bind(final QBUser people) {
			people_name.setText(people.getFullName());
			message.setText(people.getEmail());
			overflow_people
					.setOnClickListener(new OverflowPeopleSelectedListener(base
							.getContext(), people));
		}
	}
}
